package com.audaque.springboot.foshanupload.core.result;

import com.audaque.springboot.foshanupload.core.enums.ResultCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.*;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * @author helen
 * @since 2019/12/25
 */
@Schema(description = "响应对象")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = -1242493306307174690L;

    @Schema(description = "是否成功")
    private Boolean success;
    @Schema(description = "错误码")
    private String code;
    @Schema(description = "错误信息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    public R(T data) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        this.setCode(ResultCodeEnum.SUCCESS.getCode());
        this.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        this.setData(data);
    }


    public R(ResultCodeEnum resultErrcodeEnum, T data) {
        this.setSuccess(resultErrcodeEnum.getSuccess());
        this.setCode(resultErrcodeEnum.getCode());
        this.setMsg(resultErrcodeEnum.getMsg());
        this.setData(data);
    }
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    public static R ok() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        return r;
    }

    public static R ok(ResultCodeEnum resultErrcodeEnum) {
        R r = new R();
        r.setSuccess(resultErrcodeEnum.getSuccess());
        r.setCode(resultErrcodeEnum.getCode());
        r.setMsg(resultErrcodeEnum.getMsg());
        return r;
    }
    public static R ok(Object data) {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.FAILE.getSuccess());
        r.setCode(ResultCodeEnum.FAILE.getCode());
        r.setMsg(ResultCodeEnum.FAILE.getMsg());
        return r;
    }

    public static R error(ResultCodeEnum resultErrcodeEnum) {
        R r = new R();
        r.setSuccess(resultErrcodeEnum.getSuccess());
        r.setCode(resultErrcodeEnum.getCode());
        r.setMsg(resultErrcodeEnum.getMsg());
        return r;
    }


    public static R error(ResultCodeEnum resultErrcodeEnum, String appendErrmsg) {
        R r = new R();
        r.setSuccess(resultErrcodeEnum.getSuccess());
        r.setCode(resultErrcodeEnum.getCode());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(resultErrcodeEnum.getMsg()).append(appendErrmsg);
        r.setMsg(stringBuilder.toString());
        return r;
    }

    public static R error(String message) {
        R r = new R();
        r.setSuccess(ResultCodeEnum.FAILE.getSuccess());
        r.setCode(ResultCodeEnum.FAILE.getCode());
        r.setMsg(message);
        return r;
    }


    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

}
